package com.xidian.zhiancloud.service.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.InformationMapper;
import com.xidian.zhiancloud.dao.syset.SysAnnouncementMapper;
import com.xidian.zhiancloud.model.Information;
import com.xidian.zhiancloud.model.syset.SysAnnouncement;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IAnnouncementService extends ServiceImpl
        <SysAnnouncementMapper, SysAnnouncement>{

    @Autowired
    SysAnnouncementMapper announcementMapper;

    //消息中心
    @Autowired
    InformationMapper informationMapper;

    @Autowired
    HttpServletRequest request;

    public Page<SysAnnouncement> getAnnouncementList
            (Long companyId,BaseReqQuery reqdata){
        List<SysAnnouncement> validityList=announcementMapper.getByValidity();
        String id ="0";
        for(SysAnnouncement ann : validityList){
            id=String.valueOf(ann.getId());
        }
        List<String> list = getList(id);
        announcementMapper.updateByIdAndSta(list);
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        Page<SysAnnouncement> pagedata =announcementMapper.
                getAnnouncementList(companyId,reqdata.getSearchKey());
        return pagedata;
    }

    //和风险辨识评价计划一并-删除
    public int removeAnnouncement(String id){
        List<String> list = getList(id);
        return announcementMapper.removeAnnouncement(list);
    }

    /**
     * id放入list
     * @param id id(多个已逗号分隔)
     * @return List集合
     */
    public List<String> getList(String id) {
        List<String> list = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }

    public List<SysAnnouncement> selectByAnnouncement(String id){
        return announcementMapper.selectByAnnouncement(id);
    }

    public int addAnnouncement(SysAnnouncement announcement){
        int res = announcementMapper.addAnnouncement(announcement);
        if(res != 0){
            String[] ranges =request.getParameter
                ("ranges").split(",");
            for (int i = 0; i < ranges.length; i++) {
                Information info = new Information();
                info.setTitle(announcement.getTitle());
                info.setDepartment(String.valueOf(announcement.getCompanyId()));
                info.setStatus("0");
                info.setDetails(announcement.getContent());
                info.setSendId(announcement.getCompanyId());//发送者
                info.setTargetId(Long.valueOf(ranges[i]));//接收者
                info.setCreateTime(new Date());
                info.setUpdateTime(new Date());
                informationMapper.insertInfo(info);
            }
        }
        return res; 
    }

}
